package com.example.fszq.volleydemo1.thread;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.example.fszq.volleydemo1.utils.ImageCompress;
import com.example.fszq.volleydemo1.utils.MyUtil;


import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fszq on 2016/1/28.
 */
public class ImagePocThread extends AsyncTask<Void, Integer, Void>  {
    private boolean isCancled = false;
    private int taskCount = 0,taskIdx = 0;
    private ProgressDialog mProgressDialog;
    private String uToken = "",filePath = "",fileName = "";
    private Context context;
    private List<String> iPath = new ArrayList<>();
    private ArrayList<String> cPath = new ArrayList<>();
    private File outputImage;

    private MyUtil myUtil ;

    public ImagePocCallBack mAfterLoad;

    interface AfterLoad{
        public void onAfterLoad(List<String> cPath);
    }


    public ImagePocThread(Context context,List<String> iPath,ImagePocCallBack ipc) {
        this.context = context;
        this.iPath = iPath;
        this.mAfterLoad = ipc;
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.show(context,null,"图片处理中......");

    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (!isCancelled() && isCancled == false) // 这个地方很关键，如果不设置标志位的话，直接setCancel（true）是无效的
        {
            String iPathFile = "";
            try {
                //循环执行图片压缩操作
                for(int i = 0;i<iPath.size();i++) {
                    iPathFile = iPath.get(i);
                    Log.d("ipathfile:",iPathFile);
                    Bitmap bm = ImageCompress.getSmallBitmap(iPathFile);

                    //获取原文件的文件名
                    int st = iPathFile.lastIndexOf("/");
                    int en = iPathFile.length();
                    String oldFileName = iPathFile.substring(st+1,en);

                    //新文件路径
                    outputImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(), "vvvvv_" + oldFileName);
                    FileOutputStream fos = new FileOutputStream(outputImage);

                    //压缩图片到新路径
                    bm.compress(Bitmap.CompressFormat.JPEG, 40, fos);
                    cPath.add(outputImage.getAbsolutePath());
                }
            } catch(Exception e) {
                Log.d("compresserror:","error",e);
            }
        }
        //                publishProgress(prog); // 更新进度条

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        mProgressDialog.dismiss();
        mAfterLoad.CompressOk(cPath);
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        //mTaskItem.setProgress(values[0]); // 设置进度
        //mProgressDialog.setMessage("处理任务" + taskIdx + ":" + values[0] + "%");
    }

}
